package org.qy.star_server.controller;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.qy.star_server.core.BrokerServer;
import org.qy.star_server.core.VirtualHost;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yinger
 * @description TODO
 * @date 2024/3/31 15:36
 **/
@RestController
@RequestMapping("/v-host")
@Slf4j
public class VirtualHostController {


    @RequestMapping("/userAll")
    public List<VirtualHost> getVirtualHostList(String username) {
        List<VirtualHost> virtualHosts = BrokerServer.memory.selectAllVirtualHost();
        return virtualHosts;
    }

    @RequestMapping("/add")
    public boolean addVirtualHost(VirtualHost virtualHost) {
        try {
            if (virtualHost == null || virtualHost.getVirtualHostName() == null || virtualHost.getVirtualHostName().isEmpty()) {
                log.error("添加虚拟机失败！" + virtualHost);
                return false;
            }
            BrokerServer.disk.insertVirtualHost(virtualHost.getUsername(), virtualHost.getVirtualHostName());
            BrokerServer.memory.insertVirtualHost(virtualHost.getUsername(), virtualHost.getVirtualHostName());
        } catch (Exception e) {
            log.error("添加虚拟机失败！" + virtualHost);
            if (!(virtualHost == null || virtualHost.getVirtualHostName() == null || virtualHost.getVirtualHostName().isEmpty())) {
                BrokerServer.disk.deleteVirtualHost(virtualHost.getUsername(), virtualHost.getVirtualHostName());
                BrokerServer.memory.deleteVirtualHost(virtualHost.getUsername(), virtualHost.getVirtualHostName());
            }
            return false;
        }
        return true;
    }

    @Data
    static class Overview {
        private String virtualHostName;
        private int exchangeNumber;
        private int queueNumber;
        private int bindingNumber;
        // 该虚拟机内所有消息的数量
        private int messageNumber;
    }

    @RequestMapping("/overview")
    public List<Overview> getOverview(String username) {
        System.out.println("getOverview,username:" + username);
        // 获取所有虚拟机
        List<Overview> overviews = new ArrayList<>();
        List<VirtualHost> virtualHosts = BrokerServer.memory.selectVirtualHostByUser(username);
        for (VirtualHost virtualHost : virtualHosts) {
            Overview overview = new Overview();
            overview.setVirtualHostName(virtualHost.getVirtualHostName());
            int exchangeNumber = BrokerServer.memory.selectExchangeByVirtualHost(username, virtualHost.getVirtualHostName()).size();
            overview.setExchangeNumber(exchangeNumber);
            int queueNumber = BrokerServer.memory.selectQueueByVirtualHost(username, virtualHost.getVirtualHostName()).size();
            overview.setQueueNumber(queueNumber);
            int bindingNumber = BrokerServer.memory.selectBindingByVirtualHost(username, virtualHost.getVirtualHostName()).size();
            overview.setBindingNumber(bindingNumber);
            int messageNumber = BrokerServer.memory.selectMessageByVirtualHost(username, virtualHost.getVirtualHostName()).size();
            overview.setMessageNumber(messageNumber);
            overviews.add(overview);
        }
        return overviews;
    }
}